package com.panda.pfs.utils;

import org.apache.tomcat.util.http.fileupload.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class TransferFile {

    public static void transferFileWithSameFileName(String oldPath, String newPath) throws Exception {
        File originPath = new File(oldPath);
        if (!originPath.isDirectory()) {
            return;
        }
        File[] files = originPath.listFiles();
        for (File file : files) {
            File path = new File(newPath + FileUtil.hashPath(FileUtil.withoutSuffixName(file.getName())));
            if (!path.exists()) {
                path.mkdirs();
            }
            File targetFile = new File(newPath + FileUtil.hashPath(FileUtil.withoutSuffixName(file.getName())) + file.getName());
            System.out.println("transfer file: " + file.getAbsoluteFile() + " -> " + targetFile.getAbsolutePath());
            FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
            FileInputStream fileInputStream = new FileInputStream(file);
            IOUtils.copy(fileInputStream, fileOutputStream);
            fileInputStream.close();
            fileOutputStream.close();
        }
    }

    public static void main(String[] args) throws Exception {
        TransferFile.transferFileWithSameFileName("C://file/test", "E://file/panda");
    }
}
